package qj.game.pvz;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import qj.tool.jna.KeyboardHandler;
import qj.util.DesktopUtil;
import qj.util.ImageUtil;
import qj.util.ThreadUtil;
import qj.util.funct.P0;
import qj.util.math.Point;


public class PvzMain {
//	public static void main(String[] args) {
//		for (int i = 1; i < 999; i++) {
//			System.out.println(
//					"insert into MX_CHAPTER_UPDATE_RECORD(sid,query_date,chapter,commodity_code,cause,additional_info,trade_movement_cd,national) " +
//					"values (" + (1000000 + i) + ",'01-07-2011',20,'2001100011','" + (1000 + i) + "','" + (1000 + i) + "',3,0);");
//		}
//		System.out.println("commit;");
//	}
	
	public static void main3(String[] args) throws InterruptedException {
		java.awt.GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		List<String> fonts = Arrays.asList(ge.getAvailableFontFamilyNames());
		for (String font : fonts) {
			System.out.println(font);
		}
	}

	static int horizontalEdge = 100;
	public static void main(String[] args) throws Exception {
		
//		Dimension screenDimension = new Dimension(1280, 800);
		Dimension screenDimension = DesktopUtil.getScreenDimension();
		final Rectangle rect = new Rectangle(new Point(horizontalEdge, 119).toAwt(), new Dimension(screenDimension.width - horizontalEdge*2, screenDimension.height - 120));

		List<String> names = Arrays.asList(
				"sun2",
				"coin",
				"coin1",
				"coin2",
				"coin3",
				"coin4",
				"diamon",
				"goldCoin1",
				"goldCoin2",
				"goldCoin3",
				"sun_small"
				);

		final LinkedList<byte[][]> datas = new LinkedList<byte[][]>();
		for (String name : names) {
			byte[][] data = ImageUtil.getData(ImageUtil.getImage(PvzMain.class.getResourceAsStream("/" + name + ".png")));
			datas.add(data);
		}
		
		P0 clickF = new P0() {public void e() {
			byte[][] data = ImageUtil.getData(DesktopUtil.robot.createScreenCapture(rect));

			final LinkedList<Point> points = new LinkedList<Point>();
			for (byte[][] target : datas) {
				points.addAll(ImageUtil.allIndexOf(target, data));
			}
			LinkedList<Point> clickPoints = new LinkedList<Point>();
			for (Point point : points) {
				clickPoints.add(new Point(point.x + 4 + horizontalEdge, point.y + 7 + 119));
			}
			
			DesktopUtil.click(clickPoints);
		}};
		System.out.println("Ready");
//		while (true) {
//			clickF.e();
//			ThreadUtil.sleep(1860);
//		}
		KeyboardHandler.listenPressed(KeyboardHandler.LCTRL, ThreadUtil.async(clickF));
	}
}
